package com.qi.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qi.common.ApplyStatus;
import com.qi.mapper.ActivityProcessMapper;
import com.qi.pojo.dto.AddActProBody;
import com.qi.pojo.entity.Activity;
import com.qi.pojo.entity.ActivityProcess;
import com.qi.pojo.entity.User;
import com.qi.services.IActivityProcessService;
import com.qi.services.IActivityService;
import com.qi.services.IClubService;
import com.qi.services.IUserService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ActivityProcessServiceImpl extends ServiceImpl<ActivityProcessMapper, ActivityProcess> implements IActivityProcessService {

    @Resource
    private IUserService userService;

    @Resource
    private IClubService clubService;

    @Resource
    private IActivityService activityService;

    @Override
    public void addActivity(AddActProBody addActProBody) {
        User curUser = userService.getCurUserInfo();
        ActivityProcess ap = new ActivityProcess();
        BeanUtils.copyProperties(addActProBody, ap);
        ap.setClubId(curUser.getClubId());
        ap.setState(ApplyStatus.待审核);
        this.save(ap);
    }

    @Override
    public IPage<ActivityProcess> getPendingList(Long current, Long size) {
        IPage<ActivityProcess> activityProcessPage = new Page<>(current, size);
        LambdaQueryWrapper<ActivityProcess> queryWrapper = new LambdaQueryWrapper<ActivityProcess>()
                .eq(ActivityProcess::getState, ApplyStatus.待审核);
        activityProcessPage = page(activityProcessPage, queryWrapper);
        for (ActivityProcess record : activityProcessPage.getRecords()) {
            record.setMasterName(userService.getById(record.getMasterId()).getName());
            record.setClubName(clubService.getById(record.getClubId()).getName());
        }
        return activityProcessPage;
    }

    @Override
    public void approvalHold(Long actProId) {
        // 修改流程信息
        User curUser = userService.getCurUserInfo();
        LambdaUpdateWrapper<ActivityProcess> updateWrapper = new LambdaUpdateWrapper<ActivityProcess>()
                .set(ActivityProcess::getState, ApplyStatus.批准)
                .set(ActivityProcess::getAuditor, curUser.getName())
                .set(ActivityProcess::getAuditTime, new Date())
                .eq(ActivityProcess::getId, actProId);
        this.update(updateWrapper);
        // 添加活动信息
        Activity activity = new Activity();
        ActivityProcess activityProcess = this.getById(actProId);
        BeanUtils.copyProperties(activityProcess, activity);
        activity.setId(null);
        activityService.save(activity);
    }

    @Override
    public void rejectHold(Long actProId) {
        // 修改流程信息
        User curUser = userService.getCurUserInfo();
        LambdaUpdateWrapper<ActivityProcess> updateWrapper = new LambdaUpdateWrapper<ActivityProcess>()
                .set(ActivityProcess::getState, ApplyStatus.拒绝)
                .set(ActivityProcess::getAuditor, curUser.getName())
                .set(ActivityProcess::getAuditTime, new Date())
                .eq(ActivityProcess::getId, actProId);
        this.update(updateWrapper);
    }
}
